package com.hspedu.list_;

/**
 * @author DL5O
 * @version 1.0
 */
public class LinkedList01 {
    private static Node first;
    private static Node last;
    public static void main(String[] args) {
        //模拟一个简单的双向链表

        Node jack = new Node("jack");
        Node tom = new Node("tom");
        Node ywl = new Node("ywl");

        //连接三个节点
        // jack -> tom -> ywl
        first = jack;
        last = ywl;
        jack.next = tom;
        tom.next = ywl;
        ywl.next = null;

        ywl.pre = tom;
        tom.pre = jack;

        //从头到尾遍历
        System.out.println("====从头到尾遍历====");
        Node temp = first;

        //第一种写法
       /* while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }*/

        while (true) {
            System.out.println(temp);
            if (temp == last) {
                break;
            }
            temp = temp.next;
        }

        //重尾到头遍历
        System.out.println("====从尾到头遍历====");
        Node temp1 = last;
        while (temp1 != null) {
            System.out.println(temp1);
            temp1 = temp1.pre;
        }

       /* while (true) {
            System.out.println(temp1);
            if (temp1 == first) {
                break;
            }
            temp1 = temp1.pre;
        }*/

        //插入一个元素
        Node smith = new Node("smith");

        //在表头插入,头插法
        smith.next = first;
        first.pre = smith;
        first = smith;
        System.out.println("====插入元素后====");
        preOutput();

        //在表尾插入，尾插法

    }

    public static void preOutput(){
        Node temp = first;
        while (true) {
            System.out.println(temp);
            if (temp == last) {
                break;
            }
            temp = temp.next;
        }
    }

    public static void preInsert(Node insertNode){

    }

}

//定义一个Node类，表示Node 对象 一个双向链表的节点
class Node {
    public Object item; //真正存放数据结构的地方
    public Node next;   //指向后一个节点
    public Node pre;    //指向前一个节点

    public Node(Object name) {
        this.item = name;
    }

    @Override
    public String toString() {
        return "Node name = " + item;
    }

}